package member.controller;
 
import java.sql.SQLException;
import java.util.ArrayList;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import member.model.service.MemberManageService;
import member.vo.MemberVO;

@Controller
public class MemberController {
	
	 private MemberManageService service;
	 
	 public MemberController(MemberManageService service){
		 this.service = service;
	 }
	 
	 
	 @RequestMapping(value="/login.do")
	 public ModelAndView login(String id, String password, HttpSession session){
	  
		  ModelAndView modelAndView = null;
		  
		  try {
			  
			   MemberVO memberVO = service.getMemberById(id);
			   
			   if(memberVO != null){//id가 있는 경우
				   
				   if(memberVO.getPassword().equals(password)){//id와 password가 맞는 경우
					     session.setAttribute("memberVO", memberVO);
					     modelAndView = new ModelAndView("/login_success");
				    }else{
				    	modelAndView = new ModelAndView("/login_form","error_message",password+"는 없는 비밀번호입니다.");
				    }
				   
			   }else{  // id 가 없을 경우
				   modelAndView = new ModelAndView("/login_form","error_message",id+"는 없는 아이디입니다.");
			   }
		  } catch (SQLException e) {
			   e.printStackTrace();
			   modelAndView = new ModelAndView("/res/error","error_message",e.getMessage());
		  }
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/registerMember.do")
	 public ModelAndView registerMember(MemberVO memberVO){
		 
		  ModelAndView modelAndView = null;
		 // memberVO.setMileage(1000);
		  
		  try {
			   System.out.println(memberVO);
			   service.registerMember(memberVO);
			   modelAndView = new ModelAndView("/res/register_success","memberVO",memberVO);
		  } catch (Exception e) {
			   e.printStackTrace();
			   modelAndView = new ModelAndView("/res/error","error_message",e.getMessage());
		  } 
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/getAllMemberInfo.do")
	 public ModelAndView getAllMemberInfo(){
		  ModelAndView modelAndView = null;
		  try{
			   System.out.println("getMemberList()실행");
			   ArrayList<MemberVO> list = service.getMemberList();
			   modelAndView = new ModelAndView("/member_list","member_list",list);
		  }catch (Exception e) {
			  modelAndView = new ModelAndView("/res/error", "error_message",e.getMessage());
		  }
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/logout.do")
	 public ModelAndView logout(HttpSession session){
		 
		  ModelAndView modelAndView = null;
		  MemberVO memberVO = (MemberVO) session.getAttribute("memberVO");
		 
		  if(memberVO!=null){
			  modelAndView = new ModelAndView("/login_form");
			  session.invalidate();
		  }else{
			  modelAndView = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다.");
		  }
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/getLoginMemberInfo.do")
	 public ModelAndView getLoginMemberInfo(HttpSession session){
		 
		  ModelAndView modelAndView = null;
		  MemberVO memberVO = (MemberVO) session.getAttribute("memberVO");
		  
		  if(memberVO!=null){
			  modelAndView = new ModelAndView("/res/member_details");
		  }else{
			  modelAndView = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다.");
		  }
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/modifyForm.do")
	 public ModelAndView modifyForm(HttpSession session){
		 
		  ModelAndView modelAndView = null;
		  MemberVO memberVO = (MemberVO) session.getAttribute("memberVO");
		  
		  if(memberVO!=null){
			  modelAndView = new ModelAndView("/res/modify_form");
		  }else{
			  modelAndView = new ModelAndView("/login_form","error_message","로그인 후 이용바랍니다.");
		  }
		  return modelAndView;
	 }
	 
	 
	 @RequestMapping(value="/modifyMember.do")
	 public ModelAndView modifyMember(MemberVO memberVO, HttpSession session){
		 
		  ModelAndView modelAndView = null;
		  //System.out.println(memberVO);
		  MemberVO login_info = (MemberVO) session.getAttribute("memberVO");
		  
		  if(login_info==null){//로그인 안된 경우
			  modelAndView = new ModelAndView("/login_form","error_message","로그인 후 회원정보 수정바랍니다.");
		  }else{//로그인 된 경우
			   try {
				   // System.out.println("update처리");
				    service.modifyMember(memberVO);
				    session.setAttribute("memberVO", memberVO);
				    modelAndView = new ModelAndView("/res/member_details");
			   } catch (SQLException e) {
				    e.printStackTrace();
				    modelAndView = new ModelAndView("/res/error");
			   } 
		  }
		  return modelAndView;
	 }
	 
	 @RequestMapping(value="/searchmemberbyID.do")
	 public ModelAndView searchmemberbyID(String selectedId,  HttpSession session){
		 
		 ModelAndView modelAndView = new ModelAndView();
		 
		 System.out.println("searchmemberbyID.do 실행" );
		 System.out.println("selectedId :"+ selectedId );
		 
		  try {
			   MemberVO memberVO = service.getMemberById(selectedId);
			   System.out.println("memberVO :"+ memberVO );
			   
			   modelAndView.addObject("memberVO", memberVO);
			   modelAndView.setViewName("/res/member_info");
			  
		  } catch (SQLException e) {
			   e.printStackTrace();
			   modelAndView = new ModelAndView("/res/error","error_message",e.getMessage());
		  }
		  return modelAndView;
	 }
	 
	 
	 // 회원 탈퇴
	 @RequestMapping(value="/removeMember.do")
	 public ModelAndView removeMember(HttpSession session){
		 
		  MemberVO memberVO = (MemberVO) session.getAttribute("memberVO");
		  ModelAndView modelAndView = null;
		  
		  if(memberVO==null){//로그인 안된 경우
			  modelAndView = new ModelAndView("/login_form","error_message","로그인 후 회원 탈퇴 이용바랍니다.");
		  }else{
			   try {
				    service.removeMemberById(memberVO.getId());
				    modelAndView = new ModelAndView("/login_form","error_message","회원탈퇴가 정상적으로 처리되었습니다.");
				    session.invalidate();   //세션 소멸시키기
			   } catch (SQLException e) {
				    e.printStackTrace();
				    modelAndView = new ModelAndView("/res/error");
			   }
		  }
		  return modelAndView;
	 }
	 
	 
	 
	 @RequestMapping(value="/searchMemberByName.do")
	 public ModelAndView searchMemberByName(String name, HttpServletRequest request){
		 
		  ModelAndView modelAndView = null;
		  ArrayList<MemberVO> list = null;
		  
		  try {
			   list = service.getSearchByMemberName(name);
			   request.setAttribute("title","이름으로 검색");
			   request.setAttribute("search_value", name);
			//   System.out.println(list);
			   modelAndView = new ModelAndView("/res/search_result","searchList",list);
		  } catch (SQLException e) {
			   e.printStackTrace();
			   modelAndView = new ModelAndView("/res/error","error_messge",e.getMessage());
		  }
		  
		  return modelAndView;
	 }
	 
	/* 
	 @RequestMapping(value="/searchMemberByMileageRange.do")
	 public ModelAndView searchMemberByMileageRange(int startMileage, int endMileage,HttpServletRequest request){
		 
		  ModelAndView modelAndView = null;
		  ArrayList<MemberVO> list = null;
		  
		  try {
			   list = service.selectMemberByMileageRange(startMileage, endMileage);
			   request.setAttribute("title","마일리지검색");
			   request.setAttribute("search_value", startMileage+"~"+endMileage);
			  
			   System.out.println(list);
			   modelAndView = new ModelAndView("/res/search_result","searchList",list);
			   
		  } catch (SQLException e) {
			   e.printStackTrace();
			   modelAndView = new ModelAndView("/res/error","error_messge",e.getMessage());
		  }
		  return modelAndView;
	 }
	 */
	 
}
